package com.nuoniu.sibanyun.service.crm.call.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.crm.CrmGoodsOrder;
import com.nuoniu.sibanyun.entity.crm.bo.GoodsOrderDetailBo;
import com.nuoniu.sibanyun.entity.crm.call.CrmCallMinutes;
import com.nuoniu.sibanyun.entity.crm.call.CrmCallMinutesDetail;
import com.nuoniu.sibanyun.mapper.crm.call.CrmCallMinutesDetailMapper;
import com.nuoniu.sibanyun.service.crm.call.ICrmCallMinutesDetailService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 客户管理-话费管理_明细 服务实现类
 * </p>
 *
 * @author 白强
 * @since 2022-05-30
 */
@Service
@Log4j2
public class CrmCallMinutesDetailServiceImpl extends ServiceImpl<CrmCallMinutesDetailMapper, CrmCallMinutesDetail> implements ICrmCallMinutesDetailService {

    @Override
    public void saveFromCrmGoodsOrder( CrmCallMinutes callMinutes,CrmGoodsOrder crmGoodsOrder) {
        Integer companyId = crmGoodsOrder.getCompanyId();
        Integer goodsId = crmGoodsOrder.getGoodsId();
        String goodsName = crmGoodsOrder.getGoodsName();
        BigDecimal salePrice = crmGoodsOrder.getSalePrice();
        String goodsDetail = crmGoodsOrder.getGoodsDetail();
        Integer orderType = crmGoodsOrder.getOrderType();
        GoodsOrderDetailBo goodsOrderDetailBo = null;
        if(StringUtils.isNotBlank(goodsDetail)){
            goodsOrderDetailBo = JSON.parseObject(goodsDetail, GoodsOrderDetailBo.class);
        }
        Boolean isSetting = goodsOrderDetailBo.getIsSetting();
        Integer baseMinutes = goodsOrderDetailBo.getBaseMinutes();
        BigDecimal minutePrice = goodsOrderDetailBo.getMinutePrice();
        CrmCallMinutesDetail callMinutesDetail = this.getByGoodsId(companyId, goodsId,callMinutes.getYear(),callMinutes.getMonth());
        if(null == callMinutesDetail){
            callMinutesDetail = new CrmCallMinutesDetail();
            callMinutesDetail.setCompanyId(callMinutes.getCompanyId());
            callMinutesDetail.setCrmCallMinutesId(callMinutes.getId().intValue());
            callMinutesDetail.setCrmGoodsId(goodsId);
            callMinutesDetail.setCrmGoodsName(goodsName);
            callMinutesDetail.setIsSetting(isSetting);
            callMinutesDetail.setBaseMinutes(baseMinutes);
            callMinutesDetail.setTotalMinutes(baseMinutes);
            callMinutesDetail.setMinutePrice(minutePrice);
            callMinutesDetail.setPrice(salePrice);
            callMinutesDetail.setNumber(1);
            callMinutesDetail.setTotalAmount(salePrice);
            //续费方式 1：手动 2：自动
            callMinutesDetail.setRenewalType(1);
            if(orderType.equals(3)){
                //续费方式 1：手动 2：自动
                callMinutesDetail.setRenewalType(2);
            }
            callMinutesDetail.setYear(callMinutes.getYear());
            callMinutesDetail.setMonth(callMinutes.getMonth());
            if(isSetting){
                callMinutesDetail.setStartTime(callMinutes.getStartTime());
                callMinutesDetail.setEndTime(callMinutes.getEndTime());
            }
        }else{
            callMinutesDetail.setNumber(callMinutesDetail.getNumber() + 1);
            callMinutesDetail.setTotalAmount(callMinutesDetail.getTotalAmount().add(salePrice));
            callMinutesDetail.setTotalMinutes(callMinutesDetail.getTotalMinutes() + baseMinutes);
        }
        this.saveOrUpdate(callMinutesDetail);
    }

    @Override
    public CrmCallMinutesDetail getByGoodsId(Integer companyId, Integer goodsId,Integer year,Integer month) {
        QueryWrapper<CrmCallMinutesDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyId);
        queryWrapper.eq("crm_goods_id",goodsId);
        if(null != year){
            queryWrapper.eq("year",year);
        }
        if(null != month){
            queryWrapper.eq("month",month);
        }
        queryWrapper.last("LIMIT 1");
        CrmCallMinutesDetail callMinutesDetail = getOne(queryWrapper, false);
        return callMinutesDetail;
    }

    @Override
    public void changeRenewalType(Integer id, Integer renewalType) {
        if(null == renewalType || (1 != renewalType && 2 != renewalType)) {
            throw new NuoNiuException("参数错误：续费方式");
        }
        CrmCallMinutesDetail callMinutesDetail = getById(id);
        if(null == callMinutesDetail) {
            throw new NuoNiuException("查询不到数据");
        }
        Boolean isSetting = callMinutesDetail.getIsSetting();
        if(null == isSetting || !isSetting) {
            throw new NuoNiuException("非套餐没有续费方式");
        }
        CrmCallMinutesDetail updateCrmCallMinutesDetail = new CrmCallMinutesDetail();
        updateCrmCallMinutesDetail.setId(callMinutesDetail.getId());
        updateCrmCallMinutesDetail.setRenewalType(renewalType);
        this.updateById(updateCrmCallMinutesDetail);
    }


    @Override
    public List<CrmCallMinutesDetail> listByCrmCallMinutesId(Integer crmCallMinutesId) {
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        QueryWrapper<CrmCallMinutesDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("crm_call_minutes_id",crmCallMinutesId);
        queryWrapper.eq("year",year);
        queryWrapper.eq("month",month);
        List<CrmCallMinutesDetail> callMinutesDetailList = list(queryWrapper);
        return callMinutesDetailList;
    }


}
